package com.jesse.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.MotionEvent;

import com.jesse.dao.ButtonDao;
import com.jesse.main.R;
import com.jesse.observer.DefaultEvent;
import com.jesse.observer.ObserverBus;
import com.jesse.util.AppUtil;
import com.jesse.util.ViewUtil;

public class ActivityUtil {

	private float xDown;
	private float xUp;
	private boolean isOverFIXED;
	private final int FIXED = (int) (ViewUtil.getScreenWidth() * 0.2);
	public boolean gestrue = true;
	public boolean showNetErrorPopup = true;
	
	protected void onCreate(Context context) {
		ViewUtil.setContext(context);
		registerReceiver(context);
	}
	
	protected void onResume(Context context) {
		checkGuider();
        ViewUtil.setCurrentContext(context);
	}
	
	/**
	 * Hook
	 */
	protected void checkGuider() {
		
	}
	
	//手勢开关
	public boolean isGestrue() {
		return gestrue;
	}
	
	public void setGestrue(boolean gestrue) {
		this.gestrue = gestrue;
	}
	
	public void notShowNetErrorPopup() {
		showNetErrorPopup = false;
	}

	protected boolean dispatchTouchEvent(Context context, MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			xDown = event.getX();
			break;
		case MotionEvent.ACTION_UP:
			xDown = 0;
			xUp = 0;
			break;
		case MotionEvent.ACTION_MOVE:
			xUp = event.getX();
			isOverFIXED = xUp - xDown > FIXED ? true : false;
			if (isOverFIXED && gestrue && xDown < ViewUtil.getScreenWidth() / 8) {
				return true;
			}
			break;
		}
		return false;
	}
	
	private void registerReceiver(Context context) {
		IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(mReceiver, mFilter);
	}
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
	 	
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            try {
	            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
	            	checkNetStateAvailable(context);
	            }
            } catch (java.lang.RuntimeException e){
            	
            }
        }
    };
    
    public boolean checkNetStateAvailable(Context context) {
    	ConnectivityManager connectivityManager = (ConnectivityManager) ViewUtil.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    	NetworkInfo info = connectivityManager.getActiveNetworkInfo(); 
    	NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
        NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
       
        if(info == null || !info.isAvailable()) {
        	netError(context);
        	return false;
        } else if(info != null || info.isConnected()){
    	    netRecover();
           return true;
        }
    	return false;
    }
    
    public synchronized void netError(Context context) {
    	ViewUtil.Debug("网络不可用");
    	if (AppUtil.netStateOk) {
    		AppUtil.netStateOk = false;
        	ObserverBus.getInstance().updateEvent(DefaultEvent.NETSTATE, false);
        	popupNetSetting(context);
    	}
    }
    
    public synchronized void netRecover() {
    	ViewUtil.Debug("网络可用");
    	if (!AppUtil.netStateOk) {
    		AppUtil.netStateOk = true;
        	ObserverBus.getInstance().updateEvent(DefaultEvent.NETSTATE, true);
    	}
    }
    
    private void popupNetSetting(final Context context) {
    	if (!showNetErrorPopup) return;
    	
    	ViewUtil.popupWithTwoButton(context.getString(R.string.netError), context.getString(R.string.alert), context.getString(R.string.netSetting), 
			new ButtonDao() {
				
				@Override
				public void onClick() {
					Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
					context.startActivity(intent);
				}
				
			}, ViewUtil.getContext().getString(R.string.cancel), new ButtonDao() {
				
				@Override
				public void onClick() {
				}
		});
    }
    
    protected void finish(Context context) {
		ViewUtil.endWaiting();
		ViewUtil.closePopup();
	}
	
    protected void onDestroy(Context context) {
		ViewUtil.removeContext(context);
        ViewUtil.hideKeyBoard();
        context.unregisterReceiver(mReceiver);
    }
}
